<!DOCTYPE html>
<html lang='en'>

<head>
  <meta charset='UTF-8'>
  <meta http-equiv='X-UA-Compatible' content='IE=edge'>
  <meta name='viewport' content='width=device-width, initial-scale=1.0'>
  <title>Document</title>
  <script src='https://cdn.jsdelivr.net/npm/vue/dist/vue.js'></script>
  <style>
    #app {
      width: 600px;
      margin: 100px auto;
    }
    
    table {
      border: 1px solid #ccc;
      border-collapse: collapse;
    }
    
    td,
    th {
      padding: 10px;
      border: 1px solid #ccc;
    }
  </style>
</head>

<body>
  <div id='app'>
    <table>
      <thead>
        <tr>
          <th></th>
          <th>书籍名称</th>
          <th>出版日期</th>
          <th>价格</th>
          <th>购买数量</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(book,index) in books">
          <td>{{index+1}}</td>
          <td>{{book.name | getName}}</td>
          <td>{{book.date}}</td>
          <td>{{ book.price | getPrice}}</td>
          <td>
            <button @click="subClick(index)">-</button>
            <span>{{book.count}}</span>
            <button @click="addClick(index)">+</button>
          </td>
          <td><button @click="removeBook(index)">移除</button></td>
        </tr>
      </tbody>
    </table>
    <p>总价格：{{totalPrice | getPrice}}</p>
  </div>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        books: [{
          name: '算法导论',
          date: '2006-9',
          price: 85,
          count: 1
        }, {
          name: 'UNIX编程艺术',
          date: '2006-2',
          price: 59,
          count: 1
        }, {
          name: '编程珠玑',
          date: '2008-10',
          price: 39,
          count: 1
        }, {
          name: '代码大全',
          date: '2006-3',
          price: 128,
          count: 1
        }]
      },
      methods: {
        judgeCount(index) {
          // 判断count，count小于等于0,禁用按钮
          if (this.books[index].count < 1) {
            document.querySelectorAll('button')[index * 3].disabled = true
          } else if (this.books[index].count > 9) {
            document.querySelectorAll('button')[index * 3 + 1].disabled = true
          } else {
            document.querySelectorAll('button')[index * 3].disabled = false
            document.querySelectorAll('button')[index * 3 + 1].disabled = false
          }
        },
        subClick(index) {
          this.books[index].count--
            this.judgeCount(index)
        },
        addClick(index) {
          this.books[index].count++
            this.judgeCount(index)
        },
        removeBook(index) {
          this.books.splice(index, 1)
        }
      },
      filters: {
        getPrice(price) {
          return '￥' + price.toFixed(2)
        },
        getName(name) {
          return '《' + name + '》'
        }
      },
      computed: {
        totalPrice() {
          return this.books.reduce((total, book) => total + Number(book.price * book.count), 0)
        }
      }
    })
  </script>
</body>

</html>